package com.efounder.frame.tabbar;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class TabBarItemGradient extends LinearLayout {
	
//	private ImageView imageView;
//	private TextView tabTextView;
	private ChangeColorIconWithTextView changeColorIconWithTextView;
	private CircleTextView indicatorTextView;
	
	private String text;
	private int textSize = 15;
	private int tabColor;
	private int tabSelectedColor;
	
	private int indicatorNumber = 0;
	private int indicatorTextColor = Color.BLACK;
	private int indicatorBackgroundColor = Color.RED;
	private int indicatorTextSize = 11;
	
	//暂时写死
	private int height = 49;
	private int width = 55;
	
//	private int alpha;
	
//	public TabBarItemGradient(Context context,String text) {
//		this(context, text, null);
//	}
	
	public TabBarItemGradient(Context context, String text, Drawable drawable) {
		super(context);
		this.text = text;
		
		setOrientation(VERTICAL);
		setGravity(Gravity.CENTER);
		
		int relativeLayoutWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, getResources().getDisplayMetrics());
		int relativeLayoutHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics());
		
		
		//1 上部图片 + 指示器
		RelativeLayout relativeLayout = new RelativeLayout(context);
//		relativeLayout.setBackgroundColor(Color.BLUE);
		LinearLayout.LayoutParams relativeLayoutParams = new LinearLayout.LayoutParams(relativeLayoutWidth, relativeLayoutHeight);
		relativeLayout.setLayoutParams(relativeLayoutParams);
		//--1.1 图片
		changeColorIconWithTextView = new ChangeColorIconWithTextView(context,text,(BitmapDrawable)drawable);
		RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		imageViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		changeColorIconWithTextView.setLayoutParams(imageViewParams);
		relativeLayout.addView(changeColorIconWithTextView);
//		getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//
//			@SuppressWarnings("deprecation")
//			@SuppressLint("NewApi")
//			@Override
//			public void onGlobalLayout() {
//
//				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//					getViewTreeObserver().removeGlobalOnLayoutListener(this);
//				} else {
//					getViewTreeObserver().removeOnGlobalLayoutListener(this);
//				}
//			}
//		});
		
		//--1.2 指示器
		indicatorTextView = new CircleTextView(context);
//		final int indicatorWidth = (int)Math.ceil(imageViewWidth/2);
//		RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(indicatorWidth, indicatorWidth);
		RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		indicatorTextView.setLayoutParams(indicatorParams);
		relativeLayout.addView(indicatorTextView);
		indicatorTextView.setGravity(Gravity.CENTER);
		
		setIndicatorNumber(indicatorNumber);
		indicatorTextView.setTextColor(indicatorTextColor);
		indicatorTextView.setBackgroundColor(indicatorBackgroundColor);
		indicatorTextView.setTextSize(indicatorTextSize);
		
		addView(relativeLayout);
//		//2 下部文字
//		LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//		tabTextView = new TextView(context);
//		tabTextView.setLayoutParams(textViewParams);
//		tabTextView.setText(text);
//		tabTextView.setGravity(Gravity.CENTER);
//		addView(tabTextView);
	}
	
	

	public int getIndicatorNumber() {
		return indicatorNumber;
	}

	/**
	 * 设置指示器数字
	 * @param indicatorNumber
	 */
	public void setIndicatorNumber(int indicatorNumber) {
		this.indicatorNumber = indicatorNumber;
		if (indicatorNumber <= 0) {
			clearIndicator();
		}else {
			indicatorTextView.setNotifiText(indicatorNumber); 
			indicatorTextView.setVisibility(View.VISIBLE);
		}
	}
	
	public void clearIndicator(){
		indicatorTextView.setVisibility(View.INVISIBLE);
		indicatorNumber = 0;
	}

	public int getIndicatorTextColor() {
		return indicatorTextColor;
	}

	public void setIndicatorTextColor(int indicatorTextColor) {
		this.indicatorTextColor = indicatorTextColor;
		indicatorTextView.setTextColor(indicatorTextColor);
	}

	public int getIndicatorBackgroundColor() {
		return indicatorBackgroundColor;
	}

	public void setIndicatorBackgroundColor(int indicatorBackgroundColor) {
		this.indicatorBackgroundColor = indicatorBackgroundColor;
		indicatorTextView.setBackgroundColor(indicatorBackgroundColor);
	}

	public int getIndicatorTextSize() {
		return indicatorTextSize;
	}

	public void setIndicatorTextSize(int indicatorTextSize) {
		this.indicatorTextSize = indicatorTextSize;
		indicatorTextView.setTextSize(indicatorTextSize);
	}

	public String getTabText() {
		return text;
	}

	public void setTabText(String text) {
		this.text = text;
		changeColorIconWithTextView.setText(text);
	}


	public int getTabTextSize() {
		return textSize;
	}

	protected void setTabTextSize(int textSize) {
		this.textSize = textSize;
		changeColorIconWithTextView.setTextSize(textSize);
	}

	public int getTabColor() {
		return tabColor;
	}

	protected void setTabColor(int tabColor) {
		this.tabColor = tabColor;
		changeColorIconWithTextView.setIconColor(tabColor);
	}
	protected void setTabSelectedColor(int tabColor) {
		this.tabSelectedColor = tabColor;
		changeColorIconWithTextView.setSelectedIconColor(tabColor);
	}
	
	public void setTabAlpha(float alphaF) {
		changeColorIconWithTextView.setIconAlpha(alphaF);
	}
	
}
